package com.cyy.common.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.common.constant.Constants;
import com.cyy.common.domain.BaseEntity;
import com.cyy.common.mapper.DictTypeMapper;
import com.cyy.common.pojo.dto.DictTypeFormDto;
import com.cyy.common.pojo.entity.DictType;
import com.cyy.common.pojo.query.system.SystemDictTypeQuery;
import com.cyy.common.pojo.vo.system.SystemDictTypeVo;
import com.cyy.common.service.DictTypeService;
import com.cyy.common.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author 曹云友
 * @since 2024-04-12
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    @Override
    public IPage<SystemDictTypeVo> getPage(SystemDictTypeQuery query) {
        return baseMapper.getPage(PageUtils.getPage(), query);
    }

    @Override
    public DictTypeFormDto getDictTypeFormById(Integer id) {
        DictType byId = getById(id);
        if (Objects.isNull(byId)) {
            return null;
        }
        DictTypeFormDto formDto = new DictTypeFormDto();
        BeanUtils.copyProperties(byId, formDto);
        return formDto;
    }

    @Override
    public void addDictType(DictTypeFormDto req) {
        Assert.isFalse(StringUtils.isAnyBlank(req.getDictType(), req.getDictName()), "参数异常！！");
        // 查询是否讯在对应编码的记录
        Assert.isFalse(baseMapper.exists(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictType, req.getDictType())
        ), "存在对应编码记录，请修改后重新提交！");

        DictType dictType = new DictType();
        BeanUtils.copyProperties(req, dictType);
        dictType.setId(null);

        this.save(dictType);
    }

    @Override
    public void updateDictType(DictTypeFormDto req) {
        Assert.isFalse(Objects.isNull(req.getId()) || StringUtils.isAnyBlank(req.getDictType(), req.getDictName()),
                "参数异常！！");

        Assert.isFalse(baseMapper.exists(Wrappers.<DictType>lambdaQuery()
                .eq(DictType::getDictType, req.getDictType())
                .ne(BaseEntity::getId, req.getId())
        ), "编码冲突，请修改后重新提交！");
        DictType dictType = new DictType();
        BeanUtils.copyProperties(req, dictType);

        this.updateById(dictType);
    }

    @Override
    public void deleteDictTypes(String ids) {
        Assert.isFalse(StringUtils.isBlank(ids), "参数错误");

        List<String> idList = Arrays.asList(ids.split(Constants.COMMA));

        baseMapper.update(Wrappers.<DictType>lambdaUpdate()
                .set(BaseEntity::getStatus, Boolean.FALSE)
                .in(BaseEntity::getId, idList));
    }
}
